package negocio.cliente.negocio;

import java.net.SocketException;
import java.util.concurrent.ConcurrentHashMap;

import base.transporte.SRSocket;
import base.transporte.SRWelcomer;

import negocio.beans.Conversa;

public class GerenteMensagem implements Runnable {
	private boolean listening;
	private Thread this_thread;
	private ConcurrentHashMap<String, Conversa> listaConversas;

	public GerenteMensagem(ConcurrentHashMap<String, Conversa> listaConversas) {
		this.listaConversas = listaConversas;
		listening = true;
	}
	
	public void start(){
		this_thread = new Thread(this);
		this_thread.start();
	}
	
	public void stop(){
		this.listening = false;
		this.this_thread.interrupt();
	}
	
	@Override
	public void run() {
		try {
			SRWelcomer welcomer = new SRWelcomer(42001, "Gerente de mensagens");
			System.out.println("Gerente rodando");
			while(listening){
				SRSocket skt = welcomer.accept();
				System.out.println("novo socket msg criado");
				GerenteMensagemExecutor gerente = new GerenteMensagemExecutor(skt, listaConversas);
				gerente.start();
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
}
